package classdemo;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

/**
 * @DESCRIPATION
 * @AUTHOR 戴超
 * @DATE 2018/6/19
 **/
public class ThreadServerTest extends Thread {
    private Socket client = null;

    public void setSocket(Socket client) {
        this.client = client;
    }

    @Override
    public void run() {
        try {
            PrintStream out = new PrintStream(client.getOutputStream());// 客户端输出流
            BufferedReader buf = new BufferedReader(new InputStreamReader(client.getInputStream()));
            boolean temp = true;
            while (temp) { //  要一直接收客户端的请求
                String str = buf.readLine(); //  接收内容
                if ("bye".equals(str)) {
                    temp = false; //  不再接收
                } else {//  线程子类
                    out.println("Server>> ：" + str); //  将内容送回客户端
                }
            }
            out.close();
            buf.close();
            client.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
